﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[usp_select_AlertHistory2]
	@NumberOfRecords int = 10
AS

SELECT  ahr.*,a.alertid FROM
(
	SELECT 
		ROW_NUMBER() OVER (ORDER BY ah.[InsertedDate] DESC) AS Row, ah.*,
		(select count(*) from AlertSubscriptionHistory ash where ash.AlertHistoryId = ah.AlertHistoryId) as AlertCount
	FROM [AlertHistory] ah
) AS [ahr] ,Alert a
WHERE [ahr].Row <= @NumberOfRecords
and ahr.alertname = a.name
